package com.action.app_landing;

import java.io.PrintWriter;
import java.io.Serializable;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.enetiy.Area;
import com.enetiy.Authcode;
import com.enetiy.Authcodeinfo;
import com.opensymphony.xwork2.ActionSupport;
import com.operation.HttpSendUrl;
import com.operation.SendMC;
import com.service.AreaService;
import com.service.AuthCodeService;
import com.util.DataUtil;
import com.util.Log4gUtil;
import com.util.Verify;
/**
 * 
 * 手机验证码  >Nf006
 * @author hzy
 *
 */

public class AuthCodeAction extends ActionSupport{
	private String phone;//手机号
	private int authCodeType;//发送验证码类型(短信: 0 ; 语音: 1 )
	private int siteAreaid;//区域
	private String params;
	

	private AuthCodeService authCodeService;
	//验证码
	public void PhoneVerification(){
		if(params!=null&&!params.equals("")){
			JSONObject datajson = new JSONObject(params);
			if(datajson.has("phone"))//手机号
				phone = datajson.getString("phone");
			if(datajson.has("authCodeType"))//发送类型(短信: 0 ; 语音: 1 )
				authCodeType = datajson.getInt("authCodeType");
		}
		Log4gUtil.Info("PhoneVerification","", "进入申请验证码param", params, "", "");
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j = DataUtil.sendJson();
		PrintWriter out=null;
		Authcode authcode = null;
		Authcodeinfo aci = new Authcodeinfo();
		String send = "-1"; //发送状态,0为发送成功,非0为发送不成功 
		try {
			out = response.getWriter();
				//判断此手机是否已有验证码;null为没有
				if((authcode=authCodeService.selectAuthCode(phone))!=null){//如数据库有此手机的验证码
					//发送
					if(authCodeType==0){//短信
						send = SendMC.getSendSingle(authcode.getPhone(), authcode.getCode());
						aci.setType(0);
					}else{//语音
						send = SendMC.getSendVoice(authcode.getPhone(), authcode.getCode());
						aci.setType(1);
					}
					if(send.equals("0")){//发送成功
						aci.setPhone(authcode.getPhone());
						aci.setCode(authcode.getCode());
						aci.setSendtime(new Date());
						aci.setAreaId(siteAreaid);
						//保存手机验证码信息
						authCodeService.insertAuthCodeInfo(aci);
						j.put("code", 1);
					}else{//发送失败 
						j.put("code", 0);
					}
				}else{//如数据库无此手机的验证码
					//生成验证码
					String r =  this.RandomCode();
					//发送
					if(authCodeType==0){//短信
						send = SendMC.getSendSingle(phone, r);//短信
						aci.setType(0);
					}else{//语音
						send=SendMC.getSendVoice(phone, r);
						aci.setType(1);
					}
					if(send.equals("0")){//发送成功
						authcode= new Authcode(r,phone);
						//添加至数据库
						Serializable i = authCodeService.insertAuthCode(authcode);
						//计时5分钟后删除
						authcode.setId(Integer.parseInt(i.toString()));
						this.start(authcode);
						System.out.println("authcode.getPhone()------"+authcode.getPhone());
						aci.setPhone(authcode.getPhone());
						aci.setCode(authcode.getCode());
						aci.setSendtime(new Date());
						aci.setAreaId(siteAreaid);
						//保存手机验证码信息
						authCodeService.insertAuthCodeInfo(aci);
						j.put("code", 1);
					}else{
						j.put("code", 0);
					}
				}
		} catch (Exception e) {
			j.put("code", -1);
			e.printStackTrace();
		}
		Log4gUtil.Info("PhoneVerification","", "输出申请验证码param", j.toString()+"send:"+send, "", "");
		out.print(j);
	}
	
	//生成6位随机码
	private  String  RandomCode(){
		Random random = new Random();
		char[] c = "0123456789".toCharArray();
		char[] b = new char[5];
			for (int i = 0; i < 5; i++) {
				b[i] = c[random.nextInt(9)];
			}
		return new String(b);
	}
	
	
	private  void  start(final Authcode a){
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				 playSound();
			}
		
		private void playSound() {
				try {
					authCodeService.deleteAuthCode(a);//5分钟后删除
					timer.cancel();
				} catch (Exception e) {
					e.printStackTrace();
				}
			
	        }
	    }, 300000);
	}
	
	
	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setSiteAreaid(int siteAreaid) {
		this.siteAreaid = siteAreaid;
	}

	public void setAuthCodeService(AuthCodeService authCodeService) {
		this.authCodeService = authCodeService;
	}

	public void setAuthCodeType(int authCodeType) {
		this.authCodeType = authCodeType;
	}
	public void setParams(String params) {
		this.params = params;
	}
	
	
}
